﻿Imports QuantumBitDesigns.Core
Imports System.Collections.ObjectModel
Imports System.Threading

Public Module SyncExtensions

    <System.Runtime.CompilerServices.Extension()> _
    Public Sub Sync(Of TItem)(ByVal targetItems As ObservableList(Of TItem), ByVal sourceItems As IEnumerable(Of TItem))
        If sourceItems Is Nothing Then
            Return
        End If
        Dim l = targetItems.AcquireLock
        '--------------------------------
        ' Find items in source that are not in target
        Dim itemsToAdd As New Collection(Of TItem)
        For Each Item In sourceItems
            If Not targetItems.Contains(Item) Then
                itemsToAdd.Add(Item)
            End If
        Next
        ' Find tags in target that should not be in source 
        Dim itemsToRemove As New Collection(Of TItem)
        For Each Item In targetItems
            If Not sourceItems.Contains(Item) Then
                itemsToRemove.Add(Item)
            End If
        Next
        '--------------------------------
        ' Apply all removes
        For Each Item In itemsToRemove
            targetItems.Remove(Item)
        Next
        ' Apply all adds
        For Each Item In itemsToAdd
            targetItems.Add(Item)
        Next
        '--------------------------------
        ' Dispose Timer
        l.Dispose()
    End Sub

    <System.Runtime.CompilerServices.Extension()> _
Public Sub Sync(Of TItem)(ByVal targetItems As ObservableList(Of TItem), ByVal sourceItems As List(Of TItem))
        If sourceItems Is Nothing Then
            Return
        End If
        Dim l = targetItems.AcquireLock
        '--------------------------------
        ' Find items in source that are not in target
        Dim itemsToAdd As New Collection(Of TItem)
        For Each Item In sourceItems
            If Not targetItems.Contains(Item) Then
                itemsToAdd.Add(Item)
            End If
        Next
        ' Find tags in target that should not be in source 
        Dim itemsToRemove As New Collection(Of TItem)
        For Each Item In targetItems
            If Not sourceItems.Contains(Item) Then
                itemsToRemove.Add(Item)
            End If
        Next
        '--------------------------------
        ' Apply all removes
        For Each Item In itemsToRemove
            targetItems.Remove(Item)
        Next
        ' Apply all adds
        For Each Item In itemsToAdd
            targetItems.Add(Item)
        Next
        '--------------------------------
        ' Dispose Timer
        l.Dispose()
    End Sub

    <System.Runtime.CompilerServices.Extension()> _
    Public Sub Sync(Of TItem)(ByVal targetItems As ObservableList(Of TItem), ByVal sourceItems As ObservableList(Of TItem))
        If sourceItems Is Nothing Then
            Return
        End If
        Dim l = targetItems.AcquireLock

        ' Find items in source that are not in target
        Dim itemsToAdd As New Collection(Of TItem)
        For Each Item In sourceItems
            If Not targetItems.Contains(Item) Then
                itemsToAdd.Add(Item)
            End If
        Next
        ' Apply all adds
        For Each Item In itemsToAdd
            targetItems.Add(Item)
        Next
        ' Find tags in target that should not be in source 
        Dim itemsToRemove As New Collection(Of TItem)
        For Each Item In targetItems
            If Not sourceItems.Contains(Item) Then
                itemsToRemove.Add(Item)
            End If
        Next
        ' Apply all removes
        For Each Item In itemsToRemove
            targetItems.Remove(Item)
        Next
        ' Dispose Timer
        l.Dispose()
    End Sub

End Module